package com.light.gulimall.product.controller;

import com.light.common.utils.PageUtils;
import com.light.common.utils.R;
import com.light.gulimall.product.dto.AttrDTO;
import com.light.gulimall.product.entity.AttrEntity;
import com.light.gulimall.product.entity.AttrGroupEntity;
import com.light.gulimall.product.service.AttrAttrgroupRelationService;
import com.light.gulimall.product.service.AttrGroupService;
import com.light.gulimall.product.service.AttrService;
import com.light.gulimall.product.vo.AttrGroupVO;
import com.light.gulimall.product.vo.AttrVO;
import com.light.gulimall.product.vo.WithAttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author minmin
 * @email 980680541@gmail.com
 * @date 2024-07-16 19:47:27
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupVO vo = this.attrGroupService.getInfoById(attrGroupId);

        return R.ok().put("attrGroup", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取指定分组关联的所有属性
     *
     * @param attrgroupId pms_attr_group 属性分组表 主键id
     * @return 查询结果
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrs =  attrGroupService.getRelation(attrgroupId);
        return R.ok().put("data",attrs);
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *
     * @param attrgroupId pms_attr_group 属性分组表 主键id
     * @return 分页信息
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoattrRelation(@RequestParam Map<String, Object> params,
                               @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils pageUtils = attrService.getNoattrRelation(params,attrgroupId);
        if (CollectionUtils.isEmpty(pageUtils.getList())){
            return R.ok().put("page",null);
        }
        return R.ok().put("page",pageUtils);
    }

    /**
     * 添加属性与分组关联关系
     * @param dto [{
     *   "attrGroupId": 0, //分组id
     *   "attrId": 0, //属性id
     * }]
     * @return [{
     *   "attrGroupId": 0, //分组id
     *   "attrId": 0, //属性id
     * }]
     */
    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrDTO> dto){
        attrAttrgroupRelationService.saveRelation(dto);
        return R.ok();
    }

    /**
     * 删除属性与分组的关联关系
     * @param dto [{"attrId":1,"attrGroupId":2}]
     * @return {
     * 	"msg": "success",
     * 	"code": 0
     * }
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrDTO> dto){
        attrAttrgroupRelationService.deleteRelation(dto);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId 所属分类id
     * @return  查询结果集
     */
    @GetMapping("/{catelogId}/withattr")
    public R getWithAttr(@PathVariable("catelogId") Long catelogId) {
       List<WithAttrVO> vo = attrGroupService.getWithAttr(catelogId);
       return R.ok().put("data",vo);
    }

}
